﻿using NicoView.Model.Service;
using NicoView.Properties;
using NicoView.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Input;

namespace NicoView.View.CommentScroll
{
    public sealed class LiveCommentScrollContext:CommentScrollContext
    {
        public LiveCommentScrollContext(NicoViewCommentListViewModelBase viewModel, ScrollViewer viewer)
            : base(viewModel, viewer) 
        {
            ScrollFlag = true;
            _beforeCommentCount = 0;


            _autoScroll = false;
        }

        private int _beforeCommentCount;


        private bool _autoScroll;

        public override void OnScrollChanged(ScrollChangedEventArgs e,int commentCount)
        {

            if (_checkAutoScroll()) ScrollToBottom();
            if (ScrollViewer.ExtentHeight == 0 || (e.ExtentHeightChange == 0 && ScrollViewer.ExtentHeight <= ScrollViewer.VerticalOffset + ScrollViewer.ViewportHeight))
            {
                _setAutoScroll(true);
            }

            if (_beforeCommentCount != commentCount) _beforeCommentCount = commentCount;
            

        }

        private void _setAutoScroll(bool flag)
        {

                _autoScroll = flag;
            
        }

        private bool _checkAutoScroll()
        {
            lock (this)
            {
                return _autoScroll;
            }
        }

        private  void ScrollToBottom()
        {
            ScrollViewer.ScrollToBottom();

        }

        internal override void OnLayoutUpdated(EventArgs e,int commentCount )
        {
            
        }

        internal override void OnScroll(ScrollEventArgs e, int count)
        {

            if ( e.NewValue < ScrollViewer.ExtentHeight)
            {
                _setAutoScroll(false);
            }
            
        }

        internal override void OnMouseWheel(MouseWheelEventArgs e, int count)
        {

            if (e.Delta > 0)
            {
                _setAutoScroll(false);
                
            }
        }

        internal override void OnLineUpClick(RoutedEventArgs e, int p)
        {
            _setAutoScroll(false);
        }

        internal override void OnKeyDown(KeyEventArgs e, int p)
        {
            if (e.Key == Key.Up)
            {
                _setAutoScroll(false);
               
                ScrollViewer.ScrollToVerticalOffset(ScrollViewer.VerticalOffset - 1);
            }
        }

        internal override void OnRichTextMouseDown(MouseButtonEventArgs e, ItemCollection itemCollection, int count)
        {
            //_setAutoScroll(false);
        }

        internal override void OnSelectionChange(SelectionChangedEventArgs e, ItemCollection itemCollection, int count)
        {
           
        }
    }
}
